//var year_data = new Array(12);
//var month_data = new Array(31);
//var type_data = new Array();
// millisecond of year = 3153600000
// millisecond of month = 262800000
// so ... 2890800000

function get_date_before(date_end)
{
//    console.log("date_end : " + date_end);
    var date_start = new Date ;
    date_start.setTime(date_end.getTime() - 28908000000) ;
    return date_start ;
}

function get_date_after(date_start)
{
    var date_end = new Date ;
    date_end.setTime(date_start.getTime() + 28908000000) ;
    return date_end ;
}

///////////////////////////////////////////////////////////////////

var example_month =
        [
            {"FDAY":6,"FMONTH":1,"FSUM":333,"FYEAR":2013}
            ,{"FDAY":7,"FMONTH":1,"FSUM":555,"FYEAR":2013}
            ,{"FDAY":11,"FMONTH":1,"FSUM":555,"FYEAR":2013}
            ,{"FDAY":22,"FMONTH":1,"FSUM":555,"FYEAR":2013}
        ]

function month_expenses(db_data)
{
    var month_data = new Array(31);
    for (var i=0; i<db_data.length; i++)
    {
        month_data[db_data[i].FDAY - 1] = db_data[i] ;
    }
    return month_data ;
}
////////////////////////////////////////////////////////////////

var example_year_income =
        [
            {"FMONTH":1,"FSUM":33333,"FYEAR":2012}
            ,{"FMONTH":2,"FSUM":33333,"FYEAR":2012}
            ,{"FMONTH":3,"FSUM":33333,"FYEAR":2012}
            ,{"FMONTH":4,"FSUM":33333,"FYEAR":2012}
            ,{"FMONTH":10,"FSUM":33333,"FYEAR":2012}
            ,{"FMONTH":1,"FSUM":33333,"FYEAR":2013}
        ]

function year_income(db_data, date_start, date_end)
{
    var year_data = new Array(12);
    var gap ;
    for (var i=0; i<db_data.length; i++)
    {
        if (db_data[i].FYEAR == date_start.getFullYear())
        {
            gap = db_data[i].FMONTH - date_start.getMonth() - 1 ;
            year_data[gap] = db_data[i] ;
        }
        else
        {
            gap = date_end.getMonth() + 1 - db_data[i].FMONTH ;
            year_data[11 - gap] = db_data[i] ;
        }
//        year_data[db_data[i].FMONTH - 1] = db_data[i] ;

    }
    return year_data ;
}
///////////////////////////////////////////////////////////

var example_year_expenses =
        [
            {"FMONTH":1,"FSUM":333,"FYEAR":2012}
            ,{"FMONTH":4,"FSUM":333,"FYEAR":2012}
            ,{"FMONTH":8,"FSUM":333,"FYEAR":2012}
            ,{"FMONTH":12,"FSUM":333,"FYEAR":2012}
            ,{"FMONTH":1,"FSUM":333,"FYEAR":2013}
        ]

function year_expenses(db_data, date_start, date_end)
{
    var year_data = new Array(12);
    var gap ;
    for (var i=0; i<db_data.length; i++)
    {
        if (db_data[i].FYEAR == date_start.getFullYear())
        {
            gap = db_data[i].FMONTH - date_start.getMonth() - 1 ;
            year_data[gap] = db_data[i] ;
        }
        else
        {
            gap = date_end.getMonth() + 1 - db_data[i].FMONTH ;
            year_data[11 - gap] = db_data[i] ;
        }
//        year_data[db_data[i].FMONTH - 1] = db_data[i] ;

    }
    return year_data ;
}
///////////////////////////////////////////////////////////

var example_rate_income =
        [
            {"fitypeid":1,"frate":0.6666666666666666,"fsum":199998,"ftsum":133332}
            ,{"fitypeid":2,"frate":0.16666666666666666,"fsum":199998,"ftsum":33333}
            ,{"fitypeid":3,"frate":0.16666666666666666,"fsum":199998,"ftsum":33333}
        ]

var example_rate_expenses =
        [
            {"fptypeid":1,"frate":0.4,"fsum":1665,"ftsum":666}
            ,{"fptypeid":3,"frate":0.2,"fsum":1665,"ftsum":333}
            ,{"fptypeid":4,"frate":0.2,"fsum":1665,"ftsum":333}
            ,{"fptypeid":5,"frate":0.2,"fsum":1665,"ftsum":333}
        ]
//////////////////////////////////////////////////////////////

function itypeName(typeid)
{
    if (typeid == 1)
    {
        return lang.income_salary ;
    }
    else if (typeid == 2)
    {
        return lang.income_investment ;
    }
    else if (typeid == 3)
    {
        return lang.income_others ;
    }
}

function ptypeName(typeid)
{
    if (typeid == 1)
    {
        return lang.expenses_food ;
    }
    else if (typeid == 2)
    {
        return lang.expenses_living ;
    }
    else if (typeid == 3)
    {
        return lang.expenses_entertainment ;
    }
    else if (typeid == 4)
    {
        return lang.expenses_investment ;
    }
    else if (typeid == 5)
    {
        return lang.expenses_travel ;
    }
    else if (typeid == 6)
    {
        return lang.expenses_others ;
    }
}

//function findMaxIncome(db_data)
//{
//    var sum = db_data[0].fsum ;
//    var maxtsum = db_data[0].ftsum ;
//    var maxrate = db_data[0].frate ;
//    var fitypeid = db_data[0].fitypeid ;
//    for (var i=1; i<db_data.length; i++)
//    {
//        if (db_data[i].FSUM > maxsum)
//        {
//            sum = db_data[0].fsum ;
//            maxtsum = db_data[0].ftsum ;
//            maxrate = db_data[0].frate ;
//            fitypeid = db_data[0].fitypeid ;
//        }
//    }
//    return {"FSUM": maxsum, "FMONTH": maxmonth, "FYEAR": maxyear} ;
//}

function findMaxYear(db_data)
{
    var maxsum = db_data[0].FSUM ;
    var maxmonth = db_data[0].FMONTH ;
    var maxyear = db_data[0].FYEAR ;
//    maxsum = db_data[0].FSUM ;
    for (var i=1; i<db_data.length; i++)
    {
        if (db_data[i].FSUM > maxsum)
        {
            maxsum = db_data[i].FSUM;
            maxmonth = db_data[i].FMONTH ;
            maxyear = db_data[i].FYEAR ;
        }
    }
    return {"FSUM": maxsum, "FMONTH": maxmonth, "FYEAR": maxyear} ;
}

function findMinYear(db_data)
{
    var minsum = db_data[0].FSUM ;
    var minmonth = db_data[0].FMONTH ;
    var minyear = db_data[0].FYEAR ;
//    maxsum = db_data[0].FSUM ;
    for (var i=1; i<db_data.length; i++)
    {
        if (db_data[i].FSUM < minsum)
        {
            minsum = db_data[i].FSUM;
            minmonth = db_data[i].FMONTH ;
            minyear = db_data[i].FYEAR ;
        }
    }
    return {"FSUM": minsum, "FMONTH": minmonth, "FYEAR": minyear} ;
}

function findMaxMonth(db_data)
{
    var maxsum = db_data[0].FSUM ;
    var maxmonth = db_data[0].FMONTH ;
    var maxyear = db_data[0].FYEAR ;
    var maxday = db_data[0].FDAY ;
//    maxsum = db_data[0].FSUM ;
    for (var i=1; i<db_data.length; i++)
    {
        if (db_data[i].FSUM > maxsum)
        {
            maxsum = db_data[i].FSUM;
            maxmonth = db_data[i].FMONTH ;
            maxyear = db_data[i].FYEAR ;
            maxday = db_data[i].FDAY ;
        }
    }
    return {"FSUM": maxsum, "FDAY": maxday, "FMONTH": maxmonth, "FYEAR": maxyear} ;
}

function findMinMonth(db_data)
{
    var minsum = db_data[0].FSUM ;
    var minmonth = db_data[0].FMONTH ;
    var minyear = db_data[0].FYEAR ;
    var minday = db_data[0].FDAY ;
//    maxsum = db_data[0].FSUM ;
    for (var i=1; i<db_data.length; i++)
    {
        if (db_data[i].FSUM < minsum)
        {
            minsum = db_data[i].FSUM;
            minmonth = db_data[i].FMONTH ;
            minyear = db_data[i].FYEAR ;
            minday = db_data[i].FDAY ;
        }
    }
    return {"FSUM": minsum, "FDAY": minday, "FMONTH": minmonth, "FYEAR": minyear} ;
}
